package org.micode.libs.wxapp;

import cn.binarywang.wx.miniapp.api.WxMaQrcodeService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaCodeLineColor;
import org.micode.common.utils.Base64Utils;
import org.micode.libs.wxapp.vo.WxQrCodeVO;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class WxQrcodeHandler {

    public static WxQrCodeVO createWxaCodeUnlimit(String scene, String wxaAppId, String wxaPath,
                                                  WxMaHandler wxMaHandler) {
        WxMaService wxMaService = wxMaHandler.getWxMaService(wxaAppId);
        try {
            // 获取小程序二维码生成实例
            WxMaQrcodeService wxMaQrcodeService = wxMaService.getQrcodeService();

            // 设置小程序二维码线条颜色为黑色
            WxMaCodeLineColor lineColor = new WxMaCodeLineColor("0", "0", "0");

            // 生成二维码图片字节流
            byte[] qrCodeBytes = null;
            int width = 200;
            boolean autoColor = false;
            boolean isHyaline = false;
            // String scene, String page, int width, boolean autoColor, WxMaCodeLineColor lineColor, boolean isHyaline
            qrCodeBytes = wxMaQrcodeService.createWxaCodeUnlimitBytes(
                    scene, wxaPath, width, autoColor, lineColor, isHyaline);
            String base64Str = Base64Utils.byte2Base64StringFun(qrCodeBytes);

            WxQrCodeVO ret = new WxQrCodeVO();
            ret.setAppId(wxaAppId);
            ret.setWidth(width);
            ret.setPage(wxaPath);
            ret.setScene(scene);
            ret.setQrCodeBase64(base64Str);
            return ret;
        } catch (Exception e) {
            log.error("Error generating applet code", e);
        }
        return null;
    }

}
